// pages/seckill/seckill.js
const util = require('../../utils/util.js');
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        currentTab: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getSeckillGoodsListData();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    getSeckillGoodsListData: function() {
        var that = this;
        // request
        var params = {
            userID: app.globalData.userID
        }
        app.network.requestPost(app.route.ROUTE_GET_SECKILL_GOODS_LIST, params, '正在获取作业', function (res) {
            //res就是我们请求接口返回的数据
            if (res.success) {
                var _seckillList = res.context.seckillList;
                for (var index in _seckillList) {
                    var currentTime = new Date();
                    var startTime = util.formatDate(currentTime) + ' ' + _seckillList[index].seckillStartTime;
                    var endTime = util.formatDate(currentTime) + ' ' + _seckillList[index].seckillEndTime;
                    var d1 = new Date(Date.parse(startTime)); 
                    var d2 = new Date(Date.parse(endTime)); 
                    if (currentTime > d1 && currentTime < d2) {
                        that.setData({
                            currentTab: index
                        })
                    }
                }
                that.setData({
                    seckillList: _seckillList
                })
            } else {
                wx.showToast({
                    title: res.msg,
                })
            }
        }, function (res) {
            wx.showToast({
                title: '加载数据失败',
            })
        })
    },
    //点击切换
    clickTab: function (e) {
        var that = this;
        if (this.data.currentTab === e.currentTarget.dataset.current) {
            return false;
        } else {
            that.setData({
                currentTab: e.currentTarget.dataset.current 
            })
        }
    },
    toGoodsDetail: function(e) {
        var _goodsId = e.currentTarget.dataset.goodsid;
        wx.navigateTo({
            url: '../goodsDetail/goodsDetail?goodsId=' + _goodsId,
        })
    }
})